package cn.edu.jxust.controller.api.sysresource;

import cn.edu.jxust.common.domain.entity.SysResource;
import cn.edu.jxust.common.domain.dto.SysResourceVo;
import cn.edu.jxust.common.domain.web.api.sysresource.IAuthorityController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.ResourceService;
import cn.edu.jxust.core.infrastructure.util.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * <p>
 *  限相关前端控制器
 * </p>
 *
 * @author QiuKangming
 * @since 2020-06-29
 * @description 权限管理相关接口
 *      1. 权限管理: 获取权限列表, 只有超级管理员和局级管理员可以获取权限列表,
 */

@Slf4j
@RestController
public class AuthorityController implements IAuthorityController {

    @Resource
    private ResourceService resourceService;

    @Override
    public Result getAuthorityList(Integer roleId){

        try{
            // roleId 是需要分配权限的 roleId
            return resourceService.getAuthorityTreeSetAuthced(roleId);
        }catch (Exception e){
            log.error("AuthorityController --> getAuthorityList: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getCurrRoleAuthorityList(Integer roleId){

        try{
            //roleId 为当前登录用户的角色 Id
            return resourceService.getAuthorityTreeByRoleId(roleId);
        }catch (Exception e){
            log.error("AuthorityController --> addAuthc: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result addAuthc(SysResourceVo sysResourceVo, BindingResult result){

        if(result.hasErrors() || sysResourceVo.getCreateUsername() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return resourceService.addResource(sysResourceVo);
        }catch (Exception e){
            log.error("AuthorityController --> addAuthc: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delAuthc(Integer id){

        try{
            SysResource resource = resourceService.getById(id);
            //如果是按钮, 则不执行 || 后面的查询操作, 如果不是按钮, 则 || 左边为false, 还会执行右边的查询数量,
            //如果为0代表没有子资源, 直接删除, 如果不等于0, 则执行else分支的语句
            if(resource.getResourceType().equals(Const.OPE_RESOURCE) || resourceService.selectSubPermsByPermissionId(id) == 0){//是按钮
                return resourceService.delResource(id);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该资源存在下级资源，无法删除！");
            }
        }catch (Exception e){
            log.error("AuthorityController --> delAuthc: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifyAuthc(SysResourceVo sysResourceVo, BindingResult result){

        if(result.hasErrors() || sysResourceVo.getId() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return resourceService.modifyResource(sysResourceVo);
        }catch (Exception e){
            log.error("AuthorityController --> modifyAuthc: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getParentAuthc(){

        try{
            return resourceService.getParentAuthcList();
        }catch (Exception e){
            log.error("AuthorityController --> modifyAuthc: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}

